
#region Imports

using System;
using System.Collections.Generic;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

#endregion

namespace Justa.Web.Generic
{
    /// <summary>
    /// 
    /// </summary>
    /// 
    /// <author>Abel Perez (java.aperez@gmail.com)</author>
    ///  
    public class PersistableCollection<T> : List<T>, IStateManager
    {
        /// <summary>
        /// 
        /// </summary>
        /// 
        private bool captured = false;

        /// <summary>
        /// 
        /// </summary>
        /// 
        public bool IsTrackingViewState
        {
            get
            {
                return captured;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// 
        public void TrackViewState()
        {
            captured = true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// 
        /// <param name="state"></param>
        /// 
        public void LoadViewState(object state)
        {
            if (state != null)
            {
                T[] items = (T[])state;
                Clear();

                foreach (T item in items)
                {
                    Add(item);
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// 
        /// <returns></returns>
        /// 
        public object SaveViewState()
        {
            return ToArray();
        }
    }
}
